home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992…tember: A ROM With a View / devSep92 / devSep92.dmg / Technical Documentation / Sample Code / Snippets / Graphics & Printing / QuickDraw / Icon Displayƒ / icldrawer.sit / bullWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1990-07-25  |  4.4 KB  |  232 lines  |  [TEXT/KAHL]

  1. #include "Color.h"
  2. #include "QuickDraw32Bit.h"
  3.  
  4. /*****
  5.  * bullWindow.c
  6.  *
  7.  *        The window routines for the Bullseye demo
  8.  *
  9.  *****/
  10.  
  11. WindowPtr    bullseyeWindow;
  12. Rect        dragRect;
  13. Rect        windowBounds = { 40, 40, 300, 500 };
  14. Rect        circleStart = {10, 10, 100, 100};
  15. int            width = 5;
  16.  
  17. /****
  18.  * SetUpWindow()
  19.  *
  20.  *    Create the Bullseye window, and open it.
  21.  *
  22.  ****/
  23.  
  24. SetUpWindow()
  25.  
  26. {
  27.     dragRect = screenBits.bounds;
  28.     
  29.     bullseyeWindow = NewWindow(0L, &windowBounds, "\pBullseye", true, noGrowDocProc, -1L, true, 0);
  30.     SetPort(bullseyeWindow);
  31. }
  32. /* end SetUpWindow */
  33.  
  34. #define NULL 0L
  35. #define BYTESIZE 8
  36. #define WIDTHHEIGHT 16
  37.  
  38. GWorldPtr    GgwPtr = NULL;
  39. Rect    PlotRect = {10,200,26,216};
  40.  
  41. Boolean RectNotInGrayRgn(r)
  42. Rect *r;
  43. {
  44.     RgnHandle rgn = GetGrayRgn();
  45.     Rect    IRect,wRect;
  46.     
  47.     IRect = (**rgn).rgnBBox;
  48.     SectRect(&IRect,r,&wRect);
  49.     
  50.     return EmptyRect(&wRect);
  51. }
  52.  
  53. FindNewDevice(wind)
  54. WindowPtr    wind;
  55. {
  56.     Rect    gRect,lRect,wRect;
  57.     GDHandle    aDevice;
  58.     GDHandle    dominateDevice = NULL;
  59.     long        gArea,cArea;
  60.     GrafPtr        gp;
  61.     RgnHandle    rgn;
  62.     
  63.     GetPort(&gp);
  64.     SetPort(wind);
  65.     
  66.     gRect = PlotRect;
  67.     LocalToGlobal((Point *)&gRect.top);
  68.     LocalToGlobal((Point *)&gRect.bottom);
  69.     
  70.     aDevice = GetDeviceList();
  71.     gArea = cArea = 0;
  72.     while ( aDevice ) {
  73.         lRect = (**aDevice).gdRect;
  74.         SectRect(&lRect,&gRect,&wRect);
  75.         
  76.         if ( !EmptyRect(&wRect) ) {
  77.             cArea = (long)(wRect.right - wRect.left) * (long)(wRect.bottom - wRect.top);
  78.         
  79.             if ( cArea > gArea ) {
  80.                 gArea = cArea;
  81.                 dominateDevice = aDevice;
  82.             }
  83.         }
  84.         
  85.         aDevice = GetNextDevice(aDevice);
  86.     }
  87.     
  88.     if ( GgwPtr && dominateDevice ) {
  89.         if ( (**((**dominateDevice).gdPMap)).pixelSize != (**(GgwPtr->portPixMap)).pixelSize)
  90.             InvalRect(&PlotRect);
  91.     }
  92.     SetPort(gp);
  93. }
  94.         
  95.         
  96.     
  97.     
  98.  
  99. MakeANewGWorld(Rect *r)
  100. {
  101.     Rect        localR;
  102.     Handle        h;
  103.     QDErr        err;
  104.     short        i;
  105.     Ptr            p,tp;
  106.     short        depth;
  107.     
  108.     
  109.     localR = *r;
  110.     
  111.     /* 
  112.        by passing a global rectangle we force NewGWorld to determine the resolution
  113.        of the device the the majority of the rectangle lies on.  Then we can use the pixel
  114.        depth to determine which icl or ics to get
  115.     */
  116.     LocalToGlobal((Point *)&localR.top);
  117.     LocalToGlobal((Point *)&localR.bottom);
  118.     
  119.     if ( RectNotInGrayRgn(&localR) )
  120.         return;
  121.  
  122.     if ( !GgwPtr ) {
  123.         if ( err = NewGWorld(&GgwPtr,0,&localR,NULL,NULL,noNewDevice) )
  124.             return NULL;
  125.     }
  126.     else
  127.     if ( err = UpdateGWorld(&GgwPtr,0,&localR,NULL,NULL,0) )
  128.            return NULL;
  129.  
  130.         
  131.     /* now grab the right depth of icl or ics */
  132.         depth = (**(GgwPtr->portPixMap)).pixelSize; 
  133.         switch ( depth ) {
  134.              
  135.             case 2: SetRect(&localR,0,0,16,16);
  136.                     DisposeGWorld(GgwPtr);
  137.                     if ( err = NewGWorld(&GgwPtr,1,&localR,NULL,NULL,0) )
  138.                            return NULL; 
  139.             case 1: h = GetResource('ics#',128);
  140.                     depth = 1;
  141.                     break;
  142.             case 4:    h = GetResource('ics4',128);
  143.                     break;
  144.             case 8:    h = GetResource('ics8',128);
  145.                     break;
  146.             default: h = GetResource('ics8',128);
  147.                      depth = 8;
  148.                      SetRect(&localR,0,0,16,16);
  149.                      DisposeGWorld(GgwPtr);
  150.                      if ( err = NewGWorld(&GgwPtr,8,&localR,NULL,NULL,0) )
  151.                            return NULL; 
  152.                      break;
  153.         }
  154.         
  155.  
  156.         
  157.         
  158.         /* we will be working with the pixel data directly so lock them */
  159.         p = GetPixBaseAddr(GgwPtr->portPixMap);
  160.         if ( p ) {
  161.             HLock(h);
  162.             tp = *h;
  163.             
  164.             /* 
  165.                 because NewGWorld usually adds a long word of padding so that
  166.                 it can more easily keep the pixel map long word aligned we
  167.                 can't just BlockMove the data directly.  Instead we have to
  168.                 move one row from our source and then advance the pointer of the
  169.                 destination to the next row
  170.             */
  171.             for (  i = 0; i < WIDTHHEIGHT; i++ ) { 
  172.                 BlockMove(tp,p,(WIDTHHEIGHT / BYTESIZE) * depth);        /* move a row */
  173.                 /*
  174.                   now advance the destination pointer to the next row.  Don't
  175.                   forget to and off he high bit which marks this as a pixelmap
  176.                  */
  177.                 p += ((**(GgwPtr->portPixMap)).rowBytes ) & 0x7fff;
  178.                 tp += (WIDTHHEIGHT / BYTESIZE) * depth; /*move the source pointer to the next row */
  179.             }
  180.             HUnlock(h);
  181.             ReleaseResource(h);
  182.         }
  183.  
  184.         
  185. }
  186.  
  187. DrawIcResources()
  188. {
  189.     Rect    r,r1;
  190.     QDErr    err;
  191.     Ptr        p;
  192.     long    numpixels;
  193.     short    i;
  194.     Ptr        tp;
  195.     
  196.     
  197.         
  198.     
  199.     r1 = PlotRect;
  200.     MakeANewGWorld(&r1);
  201.         
  202.     if ( GgwPtr ) {
  203.         r1 = (**(GgwPtr->portPixMap)).bounds;
  204.         r1.right = r1.left + 16;
  205.         CopyBits((BitMap *)(*(GgwPtr->portPixMap)),&bullseyeWindow->portBits,
  206.                     &(r1),&PlotRect,srcCopy,NULL);
  207.     }
  208. }
  209.         
  210.  
  211. /*****
  212.  * DrawBullseye()
  213.  *
  214.  *    Draws the bullseye.
  215.  *
  216.  *****/
  217.  
  218. DrawBullseye(active)
  219.  
  220.     short    active;
  221.  
  222. {
  223.     
  224.     SetPort(bullseyeWindow);
  225.     EraseRect(&bullseyeWindow->portRect);
  226.     
  227.     
  228.     
  229.     DrawIcResources();
  230.  
  231. }
  232. /* end DrawBullseye */